package cool.taomu.box.storage.test

import com.google.inject.Guice
import cool.taomu.box.guice.TaomuGuiceModule
import cool.taomu.box.guice.ann.Binder
import cool.taomu.box.guice.entity.Scope
import cool.taomu.box.storage.indexed.impl.LmdbIndexed
import cool.taomu.box.storage.indexed.inter.IIndexed
import cool.taomu.box.storage.inter.IStorage
import cool.taomu.box.storage.lmdb.LmdbDataSource
import cool.taomu.box.storage.lmdb.LmdbProvider
import cool.taomu.box.storage.lmdb.LmdbUtils
import org.junit.Test

@Binder(bind=String, named="lmdb.path", value="./")
@Binder(bind=int, named="lmdb.maxSize", value="1024")
@Binder(bind=int, named="lmdb.maxDbs", value="1")
@Binder(bind=int, named="lmdb.maxReaders", value="126")
@Binder(bind=LmdbDataSource, to=LmdbProvider, provider=true)
@Binder(bind=IStorage, to=LmdbUtils, in=Scope.SINGLETON)
class LmdbTest {
	
	var tgmodule = new TaomuGuiceModule();
	
	@Test
	def void A() {
		var module = tgmodule.build(LmdbTest)
		val guice = Guice.createInjector(module)
		for (var i = 0; i < 10000; i++) {
			new Thread([
				var lmdb = guice.getInstance(IStorage);
				lmdb.put("a", "a", ("Hello lmdb").bytes);
				println(new String(lmdb.get("a", "a")))
			]).start;
		}
	}
	
	@Test
	def void B() {
		var module = tgmodule.build(LmdbTest,LmdbIndexed)
		val guice = Guice.createInjector(module)
		var im = guice.getInstance(IIndexed);
		im.open("aa");
		im.createKeyPath("id");
		im.put("{\"id\":\"aa\",\"name\":\"Hello World\"}");
		println(im.get("aa"))
	}
}
